package com.pro.service.imple;

import com.pro.mapper.CourseMapper;
import com.pro.mapper.ProductMapper;
import com.pro.pojo.*;
import com.pro.service.CourseService;
import com.pro.utils.RedisCache;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: CourseServiceImple
 * @Author: Kai
 * @DATE: 2022/1/13
 * @TIME: 下午 04:32
 */
@Service
public class CourseServiceImple implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RedisCache redisCache;

    /* 功能描述: <br>
    * 〈查询所有课程类型〉
    *
    * @param
    * @return: java.util.List<com.pro.pojo.CourseClass>
    * @Author: Kai
    * @Date: 2022/1/13 下午 04:33
    */
    @Override
    public List<CourseClass> getAllCourseClasses() {
        List<CourseClass> allCourseClasses;
        if (redisCache.getCacheList("courseTree") != null && redisCache.getCacheList("courseTree").size() > 0){
            allCourseClasses = redisCache.getCacheList("courseTree");
        }else{
            allCourseClasses = courseMapper.getAllCourseClasses();
            redisCache.setCacheList("courseTree", allCourseClasses);
        }
        return allCourseClasses;
    }

    /* 功能描述: <br>
    * 〈设置redis缓存〉
    *
    * @param
    * @return: void
    * @Author: Kai
    * @Date: 2022/1/14 下午 05:03
    */
    public void setCache(){
        List<CourseClass> allCourseClasses = courseMapper.getAllCourseClasses();
        redisCache.setCacheList("courseTree", allCourseClasses);
    }

    /* 功能描述: <br>
    * 〈添加课程类型〉
    *
    * @param courseClass
    * @return: java.lang.Boolean
    * @Author: Kai
    * @Date: 2022/1/14 上午 10:08
    */
    @Override
    public Boolean addCourseClass(CourseClass courseClass) {
        Boolean addCourseClassBoolean = courseMapper.addCourseClass(courseClass);
        if (addCourseClassBoolean){
            setCache();
        }
        return addCourseClassBoolean;
    }

    /* 功能描述: <br>
     * 〈修改课程类型〉
     *
     * @param courseClass
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/14 上午 09:50
     */
    @Override
    public Boolean updateCourseClass(CourseClass courseClass) {
        Boolean updateCourseClassBoolean = courseMapper.updateCourseClass(courseClass);
        if (updateCourseClassBoolean){
            setCache();
        }
        return updateCourseClassBoolean;
    }

    /* 功能描述: <br>
     * 〈删除课程类型〉
     *
     * @param id
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/14 上午 09:50
     */
    @Override
    public Boolean deleteCourseClass(Integer id) throws Exception {
        if (id == null || id < 1){
            throw new Exception("id不正确");
        }
        Boolean deleteCourseClassBoolean = courseMapper.deleteCourseClass(id);
        if (deleteCourseClassBoolean){
            setCache();
        }
        return deleteCourseClassBoolean;
    }


    /* 功能描述: <br>
     * 〈查询所有课程〉
     *
     * @param course
     * @return: java.util.List<com.pro.pojo.JsCourse>
     * @Author: Kai
     * @Date: 2022/1/13 下午 04:44
     */
    @Override
    public List<JsCourse> getAllCourse(JsCourse course) {
        List<JsCourse> allCourse = courseMapper.getAllCourse(course);
        return allCourse;
    }

    /* 功能描述: <br>
     * 〈修改商品信息〉
     *
     * @param course
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/14 下午 01:53
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateCourse(JsCourse course, JsCourseJournal jsCourseJournal) throws Exception {
        Boolean updateCourseBoolean = courseMapper.updateCourse(course);
        Boolean addCourseJournal = false;
        if (jsCourseJournal != null) {
            addCourseJournal = courseMapper.addCourseJournal(jsCourseJournal);
            if (!(addCourseJournal && updateCourseBoolean)) {
                throw new Exception("修改失败");
            }
        }else {
            return updateCourseBoolean;
        }
        return true;
    }

    /* 功能描述: <br>
     * 〈根据id查看课程〉
     *
     * @param id
     * @return: com.pro.pojo.JsCourse
     * @Author: Kai
     * @Date: 2022/1/15 上午 08:47
     */
    @Override
    public JsCourse getCourseById(Integer id, Integer type) {
        JsCourse courseById = courseMapper.getCourseById(id, type);
        return courseById;
    }

    /* 功能描述: <br>
     * 〈查看课程价格历史信息〉
     *
     * @param courseJournal
     * @return: java.util.List<com.pro.pojo.CourseJournal>
     * @Author: Kai
     * @Date: 2022/1/15 下午 02:49
     */
    @Override
    public List<CourseJournal> getCourseJournal(CourseJournal courseJournal) {
        List<CourseJournal> courseJournal1 = courseMapper.getCourseJournal(courseJournal);
        return courseJournal1;
    }

    /* 功能描述: <br>
     * 〈根据id删除课程〉
     *
     * @param id
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/20 下午 02:56
     */
    @Override
    public Boolean deleteCourse(Integer id) throws Exception {
        if (id == null || id < 1){
            throw new Exception("id不正确");
        }
        Boolean deleteCourseById = courseMapper.deleteCourse(id);
        return deleteCourseById;
    }

    /* 功能描述: <br>
     * 〈添加课程信息〉
     *
     * @param course
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/23 上午 09:51
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addCourse(JsCourse course, CourseRecord courseRecord) throws Exception {
        Boolean addCourse = courseMapper.addCourse(course);
        courseRecord.setCourseId(course.getCourseId());
        Boolean addCourseRecord = productMapper.addCourseRecord(courseRecord);
        if (!(addCourse && addCourseRecord)){
            throw new Exception("添加失败");
        }
        return true;
    }

    /* 功能描述: <br>
     * 〈查询个人课程列表〉
     *
     * @param course
     * @return: java.util.List<com.pro.pojo.JsCourse>
     * @Author: Kai
     * @Date: 2022/1/26 下午 04:06
     */
    @Override
    public List<JsCourse> getCourseListsByUserId(JsCourse course) {
        List<JsCourse> courseListsByUserId = courseMapper.getCourseListsByUserId(course);
        return courseListsByUserId;
    }

    /*统计课程名称*/
    @Override
    public List<JsCourse> getTitle() {
        return courseMapper.getTitle();
    }
    /*统计课程点击次数*/
    @Override
    public List<JsCourse> getHits() {
        return courseMapper.getHits();
    }
    /*统计课程分享次数*/
    @Override
    public List<JsCourse> getFenXiang() {
        return courseMapper.getFenXiang();
    }
    /*统计课程收藏次数*/
    @Override
    public List<JsCourse> getCollection() {
        return courseMapper.getCollection();
    }
}
